# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: keyboard.py
# Time: 18:08

import allure
from airtest.core.api import *

from common.element.devices import OriginalEquipment


class TheKeyboard():
    def __init__(self, poco, log):
        self.__poco = poco
        self.__log = log
        self.Wide_high = OriginalEquipment(log).get_sccreen_size()

    def the_input_keyevent(self, key):
        '''
        键盘事件
        :param key: 按键键盘
        :return:
        '''
        with allure.step("键盘输入：{}".format(key)):
            try:
                for i in list(key):
                    keyevent("KEYCODE_%s" % i)
                self.__log.info("键盘输入：{}".format(key))
            except Exception as e:
                self.__log.error("输入：{}，键盘事件异常：{}".format(key, e))

    def gestures_password(self, swipePassword):
        """
        默认屏幕尺寸是 1080 2040，按照当前屏幕尺寸进行suof
        :param swipePassword:  按照1-9写，如 2369
        """
        with allure.step("正在使用手势密码登录:".format(swipePassword)):
            try:
                width, height = self.Wide_high
                x = 1080 / width
                y = 2340 / height
                dic = {
                    '1': [int(222 * x), int(864 * y)],
                    '2': [int(531 * x), int(881 * y)],
                    '3': [int(827 * x), int(877 * y)],
                    '4': [int(238 * x), int(1202 * y)],
                    '5': [int(535 * x), int(1191 * y)],
                    '6': [int(844 * x), int(1191 * y)],
                    '7': [int(235 * x), int(1487 * y)],
                    '8': [int(528 * x), int(1495 * y)],
                    '9': [int(832 * x), int(1484 * y)]
                }
                dev = device()
                strSwipe = []
                for i in list(swipePassword):
                    get = dic.get(i)
                    strSwipe.append(get)
                dev.swipe_along(strSwipe)
                self.__log.debug("使用手势密码【{}】登录成功".format(swipePassword))
            except Exception as e:
                self.__log.error("手势密码出现异常，错误信息：{}".format(e))

    def click_update_top(self):
        '''
        使用adb命令点击更新按钮
        :return:
        '''
        with allure.step("使用adb命令，点击下次更新按钮"):
            try:
                width, height = self.Wide_high
                x = 1080 / width * 350
                y = 2400 / height * 1418
                cmdnoom = f"adb shell input tap {x} {y}"
                os.popen(cmdnoom)
                self.__log.info("使用adb命令点击下次更新按钮，命令行：{}".format(cmdnoom))
            except Exception as e:
                self.__log.error("点击下次更新按钮异常：{}".format(e))
                raise


if __name__ == '__main__':
    from common.util.log_util import GenerateLog
    from poco.drivers.android.uiautomation import AndroidUiautomationPoco

    poco = AndroidUiautomationPoco(use_airtest_input=True,
                                   screenshot_each_action=False)
    TheKeyboard(poco, GenerateLog().logger()).click_update_top()
